<?php
global $JoinYear;
global $PrevYear;
$Year = date("Y"); $NYear = date("Y")+1; $PYear = date("Y")-1;
$JoinYear = $Year."-".$NYear;
$PrevYear = $PYear."-".$Year;
$path = drupal_get_path('module', 'document');
drupal_add_css($path . '/css/documents.css');
//drupal_add_js($path . '/js/documents.js');

function documentscategoryhome() {
global $language,$base_url;
 $url = $base_url . '/' . drupal_get_path('module', 'document');
 if($language->language=='ta'){
	$lang= '/ta';
 }else{
 	$lang='';
 }
	 $output = '<div id="viewmod_documents_'.$language->language.'" class="view-header">
		<div class="inner_tab_content">
			<div class="inner_li">
				<a class="inactive" href="'.$base_url.$lang.'/documents/department_wise/">'.t('Department wise').'</a>		
			</div>
		    <div class="inner_li">				
				<a class="view_head_sel "  href="'.$base_url.$lang.'/documents/category">'.t('Category Wise').'</a>
			</div>		
			<div class="inner_li">';
			if($language->language =='en'){		
		 		$output .= '<a class="inactive" href="'.$base_url.$lang.'/documents/atoz">'.t('A to Z').'</a>';
			}
			 $output .= '</div>			
		    <div class="inner_li">				
				<a class="inactive"  href="'.$base_url.$lang.'/documents/search">'.t('Search').'</a>
			</div>
		</div>		
		<div class="clear"></div>		
	</div>';
	my_db_change(1);
	$result = db_query("select * from document_type_mst where doct_id in (select distinct(cast(field_doccategory_value as integer)) as cateidvalue from 
						field_data_field_doccategory  a,node n,field_data_field_department d where a.entity_id=n.nid and 
						 d.entity_id=n.nid and (n.language='".$language->language."' or n.language = '') and n.vid=a.revision_id 
						 and n.vid=d.revision_id and n.status=1) order by doct_id");
	my_db_change(0);
	$op = "<div class='document_head_list'>";
	$st =1;
	foreach ($result as $dept) {
	 	if($st%2!=0) { 
			$cls = "first_clm";
		} 
		else {
			$cls = "second_clm";
		}
	
		 $op .= "<div class='".$cls." d".$dept->doct_id."'>";
		 $op .= "<img src='".$url."/images/icon_main.png' alt='More' /> "; 
		 $op .= l(trim(t($dept->doct_type_name)),$lang.'/documents/category/'.$dept->doct_id)."</div>";	 
		 
		 $st++;

	}
	$op .="</div>";
	$output .= "<div class='result_inner'>";
	$output .= $op;
	$output .= "<div class='clr'></div></div>";	
return $output;
}
function documentscatewise() {
global $language,$base_url;
 $url = $base_url . '/' . drupal_get_path('module', 'document');
 if($language->language=='ta'){
	$lang= '/ta';
 }else{
 	$lang='';
 }
		
	 $output = '<div id="viewmod_documents_'.$language->language.'" class="view-header">
		<div class="inner_tab_content">
			<div class="inner_li">
				<a class="inactive" href="'.$base_url.$lang.'/documents/department_wise/">'.t('Department wise').'</a>		
			</div>
		    <div class="inner_li">				
				<a class="view_head_sel "  href="'.$base_url.$lang.'/documents/category">'.t('Category Wise').'</a>
			</div>		
			<div class="inner_li">';
			if($language->language =='en'){		
		 		$output .= '<a class="inactive" href="'.$base_url.$lang.'/documents/atoz">'.t('A to Z').'</a>';
			}
			 $output .= '</div>			
		    <div class="inner_li">				
				<a class="inactive"  href="'.$base_url.$lang.'/documents/search">'.t('Search').'</a>
			</div>	
	</div>';
	 my_db_change(1); 
	 $catid = arg(2);
	
	$query = new EntityFieldQuery();
	$query->entityCondition('entity_type', 'node')
	  ->entityCondition('bundle', 'documents')   
	  ->fieldCondition('field_doccategory', 'value', $catid, '=')
	  ->fieldOrderBy('field_doc_year','value','DESC')
	  ->propertyCondition('status',1)
	  ->propertyCondition('language', array($GLOBALS['language']->language, LANGUAGE_NONE), 'IN')
	  ->pager(15,0); 
	  
	$result = $query->execute();
	
	
	$nid = current($result['node']);
	
	if (isset($result['node'])) {
	  $news_items_nids = array_keys($result['node']); 
	  
	  $items = entity_load('node', $news_items_nids);
	}
//print "<pre>";print_r($news_items);print "</pre>";
	 my_db_change(0);
	$output .= '<div class="result_inner">';
	$output .='<div class="res_dept"><span><strong>'.t(category_name($catid)).'</strong></span>';
	$output .='<span class="backbut"><a href="'.$base_url.$lang.'/documents/deptname">'.t('Back').'</a></span></div>';
	$output .= theme('documents_archive_theme',array('items' => $items,'depid'=>$depid,'nid'=>$nid));
	$output .='<div class = "clear"></div></div>';
	$output .=archive_year_cat();
	$output .= theme('pager');
return $output;
}
function documentscatearchivee(){
global $language,$base_url;
 $url = $base_url . '/' . drupal_get_path('module', 'document');
 $catid = arg(2);
 $currentyr = arg(3);
  if($language->language=='ta'){
	$lang= '/ta';
 }else{
 	$lang='';
 }
	
	 $output = '<div id="viewmod_documents_'.$language->language.'" class="view-header view-archive">
		<div class="inner_tab_content">
			<div class="inner_li">
				<a class="inactive" href="'.$base_url.$lang.'/documents/department_wise/">'.t('Department wise').'</a>		
			</div>
		    <div class="inner_li">				
				<a class="view_head_sel"  href="'.$base_url.$lang.'/documents/category">'.t('Category Wise').'</a>
			</div>		
			<div class="inner_li">';
			if($language->language =='en'){		
		 		$output .= '<a class="inactive" href="'.$base_url.$lang.'/documents/atoz">'.t('A to Z').'</a>';
			}
			 $output .= '</div>			
		    <div class="inner_li">				
				<a class="inactive"  href="'.$base_url.$lang.'/documents/search">'.t('Search').'</a>
			</div>
		</div>';	
		
	$output .='<div class="result_inner"><div class="res_dept"><div class="arcyear">'.t('Archive of the ').$currentyr.'</div>';
	$output .='<span><strong>'.t(category_name($catid)).'</strong></span>';
	$output .='<span class="backbut"><a href="'.$base_url.$lang.'/documents/deptname">'.t('Back').'</a></span></div>';
	my_db_change(1);

	$query = new EntityFieldQuery();
	$query->entityCondition('entity_type', 'node')
	  ->entityCondition('bundle', 'documents')   
	  ->fieldCondition('field_doccategory', 'value', $catid, '=')
	  ->fieldCondition('field_doc_year', 'value', $currentyr, '=')
	  ->fieldOrderBy('field_doc_year','value','DESC')
	  ->propertyCondition('status',1)
	  ->propertyCondition('language', array($GLOBALS['language']->language, LANGUAGE_NONE), 'IN')
	  ->pager(15,0);
	  
	$result = $query->execute();
	
	
	$nid = current($result['node']);
	
	if (isset($result['node'])) {
	  $news_items_nids = array_keys($result['node']); 
	  
	  $items = entity_load('node', $news_items_nids);
	}
	//print "<pre>";print_r($items);print "</pre>";
	my_db_change(0);
	$output .= theme('documents_archive_theme',array('items' => $items,'cat'=>$catid,'currentyr'=>$currentyr,'nid'=>$nid));
	$output .=archive_year_cat();
	$output .= theme('pager');

	return $output;
}
function archive_year_cat(){
global $language,$base_url,$user,$JoinYear,$PrevYear;
//print $JoinYear;
 $url = $base_url . '/' . drupal_get_path('module', 'documents');
   if($language->language=='ta'){
	$lang= '/ta';
 }else{
 	$lang='';
 }
    my_db_change(1); 
    $catid = arg(2);
    $title = '';
	$query = new EntityFieldQuery();
	$query->entityCondition('entity_type', 'node')
	  ->entityCondition('bundle', 'documents')   
	  ->fieldOrderBy('field_doc_year','value','DESC')
	  ->propertyCondition('status',1)
	  ->propertyCondition('language', array($GLOBALS['language']->language, LANGUAGE_NONE), 'IN');
	  
	$result = $query->execute();
	
	
	$nid = current($result['node']);
	
	if (isset($result['node'])) {
	    $news_items_nids = array_keys($result['node']); 
	  
		$news_items = entity_load('node', $news_items_nids);
	}
//print "<pre>";print_r($news_items);print "</pre>";
 	my_db_change(0);
 	$output .= '<div id="docArchive" style="text-align:right;"><span>Archive</span>';
	$i=0;
	foreach($news_items as $items){		
		if($title != $items->field_doc_year['und'][0]['value']) {
			$title = $items->field_doc_year['und'][0]['value'];
			$output .= '<a  style="padding: 10px 5px;" href="'.$base_url.$lang.'/documents/category/'.$catid.'/'.$title.'">'.$title.'</a>';		
			if($i=='5' || $i == '10' || $i == '15'){
				$output .= '<div class="clr" style="padding-top: 10px;"></div>';
			}	
			$i++;	
		}			
	}	
	$output .='</div>';
	return $output;

}
function documentsalpha($alp){
global $language,$base_url;
$url = $base_url . '/' . drupal_get_path('module', 'document');
$alp = arg(2);
 if($language->language=='ta'){
	$lang= '/ta';
 }else{
 	$lang='';
 }
	
	 $output = '<div id="viewmod_documents_'.$language->language.'" class="view-header">
		<div class="inner_tab_content">
			<div class="inner_li">
				<a class="inactive" href="'.$base_url.$lang.'/documents/department_wise/">'.t('Department wise').'</a>		
			</div>
		    <div class="inner_li">				
				<a class="inactive"  href="'.$base_url.$lang.'/documents/category">'.t('Category Wise').'</a>
			</div>		
			<div class="inner_li">';
			if($language->language =='en'){		
		 		$output .= '<a class="view_head_sel" href="'.$base_url.$lang.'/documents/atoz">'.t('A to Z').'</a>';
			}
			 $output .= '</div>				
		    <div class="inner_li">				
				<a class="inactive"  href="'.$base_url.$lang.'/documents/search">'.t('Search').'</a>
			</div>
		</div>';
	$output .= "</div><div class='view-header'>" . AtoZ_letters($alp) . "</div>";
	$output .= "<div class='result_inner'>";
	$val= strtoupper($alp);	
	 
	 my_db_change(1);
		if($val != 'ALL'){
 
			$query = new EntityFieldQuery();
			$query->entityCondition('entity_type', 'node')
			  ->entityCondition('bundle', 'documents')   
			  ->propertyCondition('status',1)
			  ->propertyCondition('title',$val.'%','LIKE')
			  ->propertyCondition('language', $language->language, '=')
			  ->propertyOrderBy('title','ASC')
			  ->pager(15,0); 
		 }
		 else {
 
			$query = new EntityFieldQuery();
			$query->entityCondition('entity_type', 'node')
			  ->entityCondition('bundle', 'documents')   
			  ->propertyCondition('status',1)
			  ->propertyCondition('language', $language->language, '=')
			  ->propertyOrderBy('title','ASC')
			  ->pager(15,0); 
		 }
		$result = $query->execute();
	
	
	$nid = current($result['node']);
	
	if (isset($result['node'])) {
	  $news_items_nids = array_keys($result['node']); 	  
	  $items = entity_load('node', $news_items_nids);
	}
	//print "<pre>";print_r($items);print "</pre>";
	my_db_change(0);
	$output .= theme('documents_archive_theme',array('items' => $items,'nid'=>$nid));
	$output .= theme('pager');
	return $output;
}
function documents_search_form(){
global $language,$base_url;
$url = $base_url . '/' . drupal_get_path('module', 'document');

 if($language->language=='ta'){
	$lang= '/ta';
 }else{
 	$lang='';
 }
	
	 $output = '<div id="viewmod_documents_'.$language->language.'" class="view-header">
		<div class="inner_tab_content">
			<div class="inner_li">
				<a class="inactive" href="'.$base_url.$lang.'/documents/department_wise/">'.t('Department wise').'</a>		
			</div>
		    <div class="inner_li">				
				<a class="inactive"  href="'.$base_url.$lang.'/documents/category">'.t('Category Wise').'</a>
			</div>		
			<div class="inner_li">';
			if($language->language =='en'){		
		 		$output .= '<a class="inactive" href="'.$base_url.$lang.'/documents/atoz">'.t('A to Z').'</a>';
			}
			 $output .= '</div>		
		    <div class="inner_li">				
				<a class="view_head_sel"  href="'.$base_url.$lang.'/documents/search">'.t('Search').'</a>
			</div>
		</div></div>';
	$output .= '<div class="result_inner_search">'. drupal_render(drupal_get_form('documents_search'));
	/*$output .= '<form id="document-search-form" method="post" accept-charset="UTF-8" action="'.$base_url.'/documents/search">
				<div><input type="hidden" value="documents" id="edit-hidden-content" name="hidden_content">
				<div id="edit-doc-cont-search-wrapper" class="form-item">
				 <label for="edit-doc-cont-search">Keyword: </label>
				 <input type="text" class="form-text" value="" size="35" id="edit-doc-cont-search" name="doc_cont_search" maxlength="128">
				 <div class="description">(Eg : Higher Education Policy Note)</div>
				</div>
				<div class="sea_go_but"><input type="submit" class="form-submit" value="Go" name="search">
				</div>
				<input type="hidden" value="document_search_form" id="edit-document-search-form" name="form_id">

				</div></form>';*/
	return $output;

}
function documents_search($form_state){	
	$keyword = trim(arg(2));
	$form['documents_search'] = array(
	'#type' => 'textfield',
	'#title' => t('Keyword'),
	'#default_value' => $keyword ? $keyword : '',
	'#size'=>'35',
	'#description'=>'Eg: Handbook of Agriculture Department',
	);    
	
	$form[ 'field1'] = array(
		'#prefix'=>'<div class="description"> (Eg:Handbook of Agriculture Department) </div>'
	);
	
    $form[ 'documents_submit' ] = array(
	'#title' => 'Submit', 
	'#type' => 'submit', 
	'#value' => 'Go', 
	'#name' => 'search',
	'#prefix' => '<div class="sea_go_but">',
	'#suffix' => '</div>',
	
	);
	
	return $form;
	
}
function documents_search_submit($form,&$form_state){
	global $base_url;	
	
	//print "form submit";  
	 
	 //print "<pre>"; print_r($form_state); print "</pre>";
	 //drupal_set_message(t('The form has been submitted.')); die;
	 
	 	if ($form_state[clicked_button]['#name'] == 'search') {		
			$str = $form_state[values][documents_search];
			if(empty($str)) {
			form_set_error('documents_search','Please Enter Key Word');
			echo 'Please Enter Key Word';
			}
			
		}
		
				
	$form_state['redirect'] = 'documents/search/'.$str; 
    return;
}


function documents_search_result_form() {
global $language,$base_url;
$url = $base_url . '/' . drupal_get_path('module', 'document');
$output = documents_search_form();
$go_keyword= trim(arg(2));
		$go_keyword= strtolower($go_keyword);	
		//$ser_string = array();
		$go_keyword = preg_replace('/[\p{Z}\s]{2,}/u', ' ', $go_keyword);
		$go_keyword=pg_escape_string($go_keyword); 
		
		$len = drupal_strlen($go_keyword);
		$keyword1 =$go_keyword; 
		
		
			// Do not count UTF-8 continuation bytes.
		 if(!empty($keyword1))
		 {
		$position[0]=strpos($keyword1,'"');
		
		 if($position[0] != "");{
		
		  $position[1]=strpos($keyword1,'"',$position[0]+1);
		
		  }
		  }
		 if(!empty($position[1]))
		 {
			//echo substr($keyword,1)."</br>";
			$k=0;
			for($i=0;$i<=$len;$i++)
			{
			
			$position[0]=strpos($keyword1,'"');
			
			if(strlen($keyword1) !=0)
			{
			if($position[0] !=0 && $position[0] !="")
			$str_not_quote[$k++] =substr($keyword1,0,$position[0]);
			if($position[0] ==false)
			$str_not_quote[$k++] =substr($keyword1,0,strlen($keyword1));
			}
			
			
			 $position[1]=strpos($keyword1,'"',$position[0]+1);
			
			 if(empty($position[1]))
			 {
			 break;
			 }
			 $sub_string_len = $position[1] - $position[0]-1;
			 
		 $ser_string[$i] = substr($keyword1,$position[0]+1, $sub_string_len);
			
			 $part_string= substr($keyword1,$position[1]+1, $len);
			
			$keyword1 =trim($part_string);
			if(empty($keyword1))
			{
			break;
			}
			}
			}
			$ser_string = array_filter_customize($ser_string);
			//print_r($ser_string);
			if(!empty($ser_string))
			{
			
				$sql_search_clause = search_keyword('LOWER(n.title)', $ser_string, $str_not_quote);
			}

	$go_val = $go_keyword; 	
	my_db_change(1);
	$query = new EntityFieldQuery();
			$query->entityCondition('entity_type', 'node')
			  ->entityCondition('bundle', 'documents')   
			  ->propertyCondition('status',1)
			  ->propertyCondition('title',$go_val.'%','LIKE')
			  //->propertyCondition('language', $language->language, '=')
			  ->propertyOrderBy('title','ASC')
			  ->pager(15,0); 
	$result = $query->execute();	
	$nid = current($result['node']);
	
	if (isset($result['node'])) {
	  $news_items_nids = array_keys($result['node']); 	  
	  $items = entity_load('node', $news_items_nids);
	}
	$query = new EntityFieldQuery();
			$query->entityCondition('entity_type', 'node')
			  ->entityCondition('bundle', 'documents')   
			  ->propertyCondition('status',1)
			  ->propertyCondition('title',$go_val.'%','LIKE')
			 // ->propertyCondition('language', $language->language, '=')
			  ->propertyOrderBy('title','ASC'); 
	$count = $query->count()->execute();
	//print "<pre>";print_r($items);print "</pre>";
	my_db_change(0);
	if($count != '0') {
		$output .='<div class="clr"><div class="record_found">'.$count.' Records Found</div></div>';
	}
	$output .= theme('documents_archive_theme',array('items' => $items,'nid'=>$nid));
	$output .= '</div>';
	$output .= theme('pager');	
return $output;

}
function AtoZ_letters($alp){
	global $base_url;
		//print_r( strtolower($alpha ));
	for($i =65; $i < 91; $i++){
			$class = "";
	
// strtolower($val );
		if(chr($i) == $alp){
			$class = " view_head_sel";
		}
		else
		{
			$class = " ";
		}
		//$linkOp .= l($val,'documents/atoz/'.$val,array('attributes'=>array('class'=>$class)))."&nbsp;&nbsp;";
		 $linkOp .= '<a class = "myclass' . $class . '"  href = "' .$base_url.'/documents/atoz/'. chr($i) .'" >' . chr($i) . '</a>';
	}
	$class = "";
	if($alp == 'All'){
		$class = " view_head_sel";
	}
	$linkOp .= '<a class = "myclass' . $class . '" href = "' .$base_url.'/documents/atoz/All" title = "All" id = "All"> All </a>';
	$StrOp = "<div class='result_inner_atoz'>";
	$StrEnd = "</div>";
	$Grey = "<div class='grey_bord_line'></div>";//$linkOp .="<br /><br />";
	return $StrOp . $linkOp . $Grey . $StrEnd;
}
?>